<?php

queue_job('get_coin_market_from_coinmarketcap', function () {/*{{{*/

    try {
        $coin_market_infos = remote_get_json('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=5000&convert=BTC', 30, 3, [
            'X-CMC_PRO_API_KEY: df063fd4-b0d5-4fa1-8bd0-a3fefa52fe65',
        ]);

        db_transaction(function () use ($coin_market_infos) {

            $record_time = datetime();

            foreach ($coin_market_infos as $info) {

                $quote_btc = $info['quote']['BTC']?:[];
                $quote_usd = $info['quote']['USD']?:[];
                $quote_cny = $info['quote']['CNY']?:[];

                db_simple_insert('coin_market', [
                    'lower_case_name' => $info['id'],
                    'name' => $info['name'],
                    'symbol' => $info['symbol'],
                    'rank' => $info['cmc_rank'],
                    'price_btc' => $quote_btc['price']?:0,
                    'price_usd' => $quote_usd['price']?:0,
                    '24h_volume_usd' => $quote_usd['volume_24h']?:0,
                    'market_cap_usd' => $quote_usd['market_cap']?:0,
                    'price_cny' => $quote_cny['price']?:0,
                    '24h_volume_cny' => $quote_cny['volume_24h']?:0,
                    'market_cap_cny' => $quote_cny['market_cap']?:0,
                    'available_supply' => $info['circulating_supply']?:0,
                    'total_supply' => $info['total_supply']?:0,
                    'percent_change_1h' => $quote_btc['percent_change_1h']?:0,
                    'percent_change_24h' => $quote_btc['percent_change_24h']?:0,
                    'percent_change_7d' => $quote_btc['percent_change_7d']?:0,
                    'record_time' => $record_time,
                ]);
            }
        });
    } catch (Exception $ex) {
        log_exception($ex);
        slack_say_to_smarty_coin($ex->getMessage());
        return false;
    }

    return true;

}, 10, [3, 3, 3, 3, 3, 3, 3, 3], 'coin_analysis');/*}}}*/

queue_job('rank_daily_rise', function () {/*{{{*/

    try {
        $coin_market = db_simple_query_first('coin_market', ['record_time <=' => datetime('8:00:00')], 'order by id desc');
        otherwise(not_empty($coin_market), '无数据');
        $last_record_time =  $coin_market['record_time'];

        $coin_market = db_simple_query_first('coin_market', ['record_time <=' => datetime($last_record_time.' -1 days')], 'order by id desc');
        otherwise(not_empty($coin_market), '无数据');
        $last_daily_record_time =  $coin_market['record_time'];

        $from_coin_markets = db_simple_query_indexed('coin_market', 'lower_case_name', ['record_time' => $last_daily_record_time]);
        $to_coin_markets = db_simple_query('coin_market', ['record_time' => $last_record_time]);

        db_transaction(function () use ($from_coin_markets, $to_coin_markets) {

            $record_time = datetime();

            foreach ($to_coin_markets as $to_coin_market) {
                $lower_case_name = $to_coin_market['lower_case_name'];

                if (array_key_exists($lower_case_name, $from_coin_markets)) {

                    $from_coin_market = $from_coin_markets[$lower_case_name];

                    if ($to_coin_market['rank'] !== $from_coin_market['rank']) {
                        db_simple_insert('rank_daily_rise', [
                            'lower_case_name' => $lower_case_name,
                            'name' => $to_coin_market['name'],
                            'symbol' => $to_coin_market['symbol'],
                            'rank_rise' =>  $from_coin_market['rank'] - $to_coin_market['rank'],
                            'from_rank' =>  $from_coin_market['rank'],
                            'to_rank' =>  $to_coin_market['rank'],
                            'record_time' => $record_time,
                            'from_record_time' => $from_coin_market['record_time'],
                            'to_record_time' => $to_coin_market['record_time'],
                        ]);
                    }
                } else {
                    db_simple_insert('new_coin_market', [
                        'lower_case_name' => $lower_case_name,
                        'name' => $to_coin_market['name'],
                        'symbol' => $to_coin_market['symbol'],
                        'record_time' => $record_time,
                    ]);
                }
            }
        });

    } catch (Exception $ex) {
        log_exception($ex);
        slack_say_to_smarty_coin($ex->getMessage());
        return false;
    }

    return true;

}, 10, [3, 3, 3, 3, 3, 3, 3, 3], 'coin_analysis');/*}}}*/

queue_job('rank_weekly_rise', function () {/*{{{*/

    try {
        $coin_market = db_simple_query_first('coin_market', ['record_time <=' => datetime('8:00:00')], 'order by id desc');
        otherwise(not_empty($coin_market), '无数据');
        $last_record_time =  $coin_market['record_time'];

        $coin_market = db_simple_query_first('coin_market', ['record_time <=' => datetime($last_record_time.' -1 weeks')], 'order by id desc');
        otherwise(not_empty($coin_market), '无数据');
        $last_week_record_time =  $coin_market['record_time'];

        $from_coin_markets = db_simple_query_indexed('coin_market', 'lower_case_name', ['record_time' => $last_week_record_time]);
        $to_coin_markets = db_simple_query('coin_market', ['record_time' => $last_record_time]);

        db_transaction(function () use ($from_coin_markets, $to_coin_markets) {

            $record_time = datetime();

            foreach ($to_coin_markets as $to_coin_market) {
                $lower_case_name = $to_coin_market['lower_case_name'];

                if (array_key_exists($lower_case_name, $from_coin_markets)) {

                    $from_coin_market = $from_coin_markets[$lower_case_name];

                    if ($to_coin_market['rank'] !== $from_coin_market['rank']) {
                        db_simple_insert('rank_weekly_rise', [
                            'lower_case_name' => $lower_case_name,
                            'name' => $to_coin_market['name'],
                            'symbol' => $to_coin_market['symbol'],
                            'rank_rise' =>  $from_coin_market['rank'] - $to_coin_market['rank'],
                            'from_rank' =>  $from_coin_market['rank'],
                            'to_rank' =>  $to_coin_market['rank'],
                            'record_time' => $record_time,
                            'from_record_time' => $from_coin_market['record_time'],
                            'to_record_time' => $to_coin_market['record_time'],
                        ]);
                    }
                }
            }
        });

    } catch (Exception $ex) {
        log_exception($ex);
        slack_say_to_smarty_coin($ex->getMessage());
        return false;
    }

    return true;

}, 10, [3, 3, 3, 3, 3, 3, 3, 3], 'coin_analysis');/*}}}*/
